package com.dzm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 作者：Dzm
 * 时间：2024-08-22-15:31
 * 功能：
 */

@Configuration
public class MyWebConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedHeader("*");   //允许任意请求头
        config.addAllowedMethod("*");   //允许任意方法
        config.addAllowedOriginPattern("*"); //允许任意的地址
        config.setAllowCredentials(true); //运行携带cookie
        configSource.registerCorsConfiguration("/**", config);
        CorsWebFilter corsWebFilter = new CorsWebFilter(configSource);
        return corsWebFilter;
    }
}
